/**
 * @file   ReceivedTTF001Msg.java
 * @date   2012-11-8上午10:59:14
 * @author liqing
 */
package com.gzceb.zhgjj.mina.TTF001;

import java.io.File;
import java.lang.reflect.Constructor;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.apache.mina.core.filterchain.IoFilter;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.transport.socket.nio.NioSocketConnector;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.context.support.XmlWebApplicationContext;


import com.gzceb.zhgjj.filecheck.AbstractFileCheck;
import com.gzceb.zhgjj.filecheck.impl.LoanAprvCheck;
import com.gzceb.zhgjj.mina.MinaConstants;
import com.gzceb.zhgjj.model.AbstractMsg;
import com.gzceb.zhgjj.model.MsgFactory;
import com.gzceb.zhgjj.model.TTF001Msg;
import com.gzceb.zhgjj.util.PropertiesUtils;
import com.gzceb.zhgjj.util.XMLUtils;

/**
 * <p>TTF001报文接收类。</p>
 *
 * @date   2012-11-8上午10:59:14 
 * @author liqing
 * @description 此类接收到中心的TTF001报文后，进行业务调度
 *              完成文件校验、生成响应文件、发送响应文件、发送响应报文的工作
 */
public class ReceivedTTF001Msg extends IoHandlerAdapter {
	
	private  final Logger logger = LoggerFactory.getLogger(getClass());
	private MsgFactory msgFactory;
		
	
	/**
     * 新连接建立的时候触发
     */
    @Override
    public void sessionCreated(IoSession session) throws Exception {
       super.sessionCreated(session);
       msgFactory = new MsgFactory();
       //System.out.println(session.getConfig().getWriteTimeout());
       session.getConfig().setWriteTimeout(PropertiesUtils.getIntValue("msgTimeOut"));
       session.getConfig().setReaderIdleTime(PropertiesUtils.getIntValue("msgTimeOut"));  //设置空闲时间10分钟，超时10分钟后关闭session
       //System.out.println(session.getConfig().getWriteTimeout());
    }

    /**
     * 新连接打开的时候触发，在sessionCreated之后被调用
     */
    @Override
    public void sessionOpened(IoSession session) throws Exception{
        super.sessionOpened(session);
    }
    /**
     * 连接被关闭的时候触发
     */
    @Override
    public void sessionClosed(IoSession session) throws Exception {
        
        super.sessionClosed(session);
    }
    /**
     * 有消息接收的时候触发
     * 关键步骤。
     */
    @Override
    public void messageReceived(IoSession session, Object message)
            throws Exception {
        String s = ((String)message).substring(4);
    	String sndMsg;
    	//sndMsg = tTF001Msg.ProcRcvMsg(message.toString());
        AbstractMsg abMsg = msgFactory.newInstance(s);
        sndMsg = abMsg.ProcRcvMsg(s);
    	session.write(sndMsg);
    }
   /**
    * 连接被闲置的时候触发
    */
    @Override
    public void sessionIdle(IoSession session, IdleStatus status)
            throws Exception {
        //super.sessionIdle(session, status);
    	logger.debug("关闭闲置session，ip：" + session.getRemoteAddress().toString());
    	session.close(false);
    }
    /**
     * 有异常抛出但是没有被catch的时候触发
     */
    @Override
    public void exceptionCaught(IoSession session,Throwable cause) throws Exception
    {
    	logger.error("关闭异常session。IP: " + session.getRemoteAddress() + "\n" + cause.getMessage());
    	session.close(true);
    	//super.exceptionCaught(session, cause);
        
    }
    /**
     * 消息发送的时候触发
     */
    @Override
    public void messageSent(IoSession session, Object message) throws Exception 
    {
        super.messageSent(session, message);
    }
    
    public static void main(String args[]){
    	
    	ApplicationContext ac = new ClassPathXmlApplicationContext("/spring/applicationContext-mina.xml");
    	
    }
}
